﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x020001FE RID: 510
[RequireComponent(typeof(Rigidbody))]
public class EjaculationBall : MonoBehaviour
{
	// Token: 0x06000E03 RID: 3587 RVA: 0x00057610 File Offset: 0x00055810
	private void Start()
	{
		this.rig = base.GetComponent<Rigidbody>();
	}

	// Token: 0x06000E04 RID: 3588 RVA: 0x00057620 File Offset: 0x00055820
	private void Update()
	{
		this.Update_Life();
		this.Update_LockTime();
		this.Update_Hitted();
		this.Update_Line();
	}

	// Token: 0x06000E05 RID: 3589 RVA: 0x0005763C File Offset: 0x0005583C
	private void Update_Life()
	{
		float a = 1f;
		float num = this.lifeTimer / this.spanOfLife;
		if (num >= this.fadeStart)
		{
			a = 1f - Mathf.InverseLerp(this.fadeStart, 1f, num);
		}
		Color color = this.mucusLine.material.color;
		color.a = a;
		this.mucusLine.material.color = color;
		this.lifeTimer += Time.deltaTime;
		if (this.lifeTimer > this.spanOfLife)
		{
			for (int i = 0; i < this.mucusObjs.Count; i++)
			{
				this.mucusObjs[i].transform.SetParent(base.transform);
			}
			UnityEngine.Object.Destroy(base.gameObject);
		}
	}

	// Token: 0x06000E06 RID: 3590 RVA: 0x00057714 File Offset: 0x00055914
	private void Update_LockTime()
	{
		if (this.lockTime > 0f)
		{
			this.lockTime -= Time.deltaTime;
			if (this.lockTime <= 0f)
			{
				this.rig.isKinematic = true;
			}
		}
	}

	// Token: 0x06000E07 RID: 3591 RVA: 0x00057754 File Offset: 0x00055954
	private void Update_Hitted()
	{
		if (this.hitted)
		{
			this.rig.drag += this.addBrake * Time.deltaTime;
			this.rig.angularDrag += this.addBrake * Time.deltaTime;
		}
	}

	// Token: 0x06000E08 RID: 3592 RVA: 0x000577A8 File Offset: 0x000559A8
	private void Update_Line()
	{
		if (this.lastHitCollider != null && !this.AddMucusPos(this.lastHitCollider))
		{
			EjaculationBall.MucusLength(base.transform.position, this.mucusObjs, this.maxLen);
			if (this.mucusLine.positionCount != this.mucusObjs.Count + 1)
			{
				this.mucusLine.positionCount = this.mucusObjs.Count + 1;
			}
			this.mucusLine.SetPosition(0, base.transform.position);
			for (int i = 0; i < this.mucusObjs.Count; i++)
			{
				this.mucusLine.SetPosition(i + 1, this.mucusObjs[i].transform.position);
			}
		}
	}

	// Token: 0x06000E09 RID: 3593 RVA: 0x00057884 File Offset: 0x00055A84
	private void OnCollisionEnter(Collision col)
	{
		this.hitted = true;
		this.lastHitCollider = col.collider;
		this.AddMucusPos(col.collider);
		this.rig.drag += this.enterBrake;
		this.rig.angularDrag += this.enterBrake;
	}

	// Token: 0x06000E0A RID: 3594 RVA: 0x000578E4 File Offset: 0x00055AE4
	private void OnCollisionStay(Collision col)
	{
		this.lastHitCollider = col.collider;
	}

	// Token: 0x06000E0B RID: 3595 RVA: 0x000578F4 File Offset: 0x00055AF4
	private bool AddMucusPos(Collider collider)
	{
		Vector3 position = base.transform.position;
		if (this.mucusObjs.Count >= 2)
		{
			Vector3 position2 = this.mucusObjs[1].transform.position;
			if (Vector3.Distance(position, position2) < this.minDis)
			{
				return false;
			}
		}
		GameObject gameObject = new GameObject("MucusLinePos");
		this.mucusObjs.Insert(0, gameObject);
		gameObject.transform.SetParent(collider.transform);
		gameObject.transform.position = base.transform.position;
		Vector3[] array = new Vector3[this.mucusObjs.Count + 1];
		array[0] = base.transform.position;
		for (int i = 0; i < this.mucusObjs.Count; i++)
		{
			array[i + 1] = this.mucusObjs[i].transform.position;
		}
		array = EjaculationBall.MucusLength(array, this.maxLen);
		this.mucusLine.positionCount = array.Length;
		this.mucusLine.SetPositions(array);
		return true;
	}

	// Token: 0x06000E0C RID: 3596 RVA: 0x00057A20 File Offset: 0x00055C20
	private static Vector3[] MucusLength(Vector3[] positions, float maxLen)
	{
		if (positions.Length < 2)
		{
			return positions;
		}
		Vector3 a = positions[0];
		float num = 0f;
		int num2 = -1;
		for (int i = 1; i < positions.Length; i++)
		{
			float num3 = Vector3.Distance(a, positions[i]);
			num += num3;
			a = positions[i];
			if (num > maxLen)
			{
				num2 = i + 1;
				float d = num - maxLen;
				Vector3 vector = positions[i - 1] - positions[i];
				vector.Normalize();
				vector *= d;
				positions[i] += vector;
				break;
			}
		}
		if (num2 == -1)
		{
			return positions;
		}
		Vector3[] array = new Vector3[num2];
		Array.Copy(positions, array, num2);
		return array;
	}

	// Token: 0x06000E0D RID: 3597 RVA: 0x00057B04 File Offset: 0x00055D04
	private static void MucusLength(Vector3 top, List<GameObject> objs, float maxLen)
	{
		if (objs.Count < 1)
		{
			return;
		}
		Vector3 a = top;
		float num = 0f;
		int num2 = -1;
		for (int i = 0; i < objs.Count; i++)
		{
			Vector3 position = objs[i].transform.position;
			float num3 = Vector3.Distance(a, position);
			num += num3;
			if (num > maxLen)
			{
				num2 = i + 1;
				float d = num - maxLen;
				Vector3 vector = a - position;
				vector.Normalize();
				vector *= d;
				objs[i].transform.position = position + vector;
				break;
			}
			a = position;
		}
		if (num2 != -1)
		{
			while (objs.Count > num2)
			{
				UnityEngine.Object.Destroy(objs[num2]);
				objs.RemoveAt(num2);
			}
		}
	}

	// Token: 0x04000E6A RID: 3690
	public float spanOfLife = 1f;

	// Token: 0x04000E6B RID: 3691
	private float lifeTimer;

	// Token: 0x04000E6C RID: 3692
	[Range(0f, 1f)]
	public float fadeStart = 0.75f;

	// Token: 0x04000E6D RID: 3693
	private Rigidbody rig;

	// Token: 0x04000E6E RID: 3694
	public float enterBrake = 20f;

	// Token: 0x04000E6F RID: 3695
	public float addBrake = 20f;

	// Token: 0x04000E70 RID: 3696
	public float lockTime = 1f;

	// Token: 0x04000E71 RID: 3697
	private bool hitted;

	// Token: 0x04000E72 RID: 3698
	[SerializeField]
	private LineRenderer mucusLine;

	// Token: 0x04000E73 RID: 3699
	private List<GameObject> mucusObjs = new List<GameObject>();

	// Token: 0x04000E74 RID: 3700
	public float minDis = 0.01f;

	// Token: 0x04000E75 RID: 3701
	public float maxLen = 1f;

	// Token: 0x04000E76 RID: 3702
	private Collider lastHitCollider;
}
